<template>
	<div>
    <!--认证失败消息提示-->
    <div class="authentication-warning" v-if="type === 'update' || type === 'change'">
      {{ warningIden }}
    </div>
    <!--上传-->
    <div class="authentication-content">
      <!--身份验证组件set-->
      <v-identity class="v-identity-component" v-if="type === 'set'" @change-params="getIdentityInfo"></v-identity>
      <!--身份验证组件change || update-->
      <v-identity class="v-identity-component" v-else-if="type === 'change' || type === 'update'" @change-params="getIdentityInfo" :parentData="identityInfo"></v-identity>
    </div>
    <!--提交按钮-->
    <div class="btn-submit">
      <button type="button" :class="{'btn-forbid-item': !allowSubmitIden}" @click="onSubmit('/personal/edit/success')" :disabled="!allowSubmitIden" v-if="changeStatus === '2' || changeStatus === '3'">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
      <button type="button" :class="{'btn-forbid-item': !allowSubmitIden}" @click="onSubmit(computeHref)" :disabled="!allowSubmitIden" v-else>{{$t('message.PersonalIdentityAuthenticationNextBtn')}}</button>
    </div>
	</div>
</template>

<script>
import VIdentity from '@/components/base/VIdentity'

export default {
  components: {
    VIdentity
  },
  data () {
    return {
      // 后台加载过来验证信息的id
      identityInfoID: '',
      studentInfoID: '',
      // 身份验证信息
      identityInfo: {
        schoolDistrict: '',
        grade: '',
        currentAddress: '',
        identifyCard: '',
        studentCard: ''
      },
      // 是否允许提交
      allowSubmitIden: false,
      // 认证失败原因
      warningIden: this.$t('message.pleaseFillIdenInfo'),
      /**
       * '000': 身份认证未上传，学校认证未上传，紧急联系人认证未上传
       * '111': 身份认证失败，学校认证失败，紧急联系人认证失败
       * '222': 身份认证成功，学校认证成功，紧急联系人认证成功
       */
      changeStatus: '',
      // 阻止重复提交
      allowSubmit: true
    }
  },
  computed: {
    // 当前页面编辑类型
    type () {
      return this.$route.params.type || 'undefined'
    },
    // 判断提交或下一步跳转的页面
    computeHref () {
      // 防止眼花
      if (this.changeStatus[0] === '0') {
        return '/personal/edit/contacts/set'
      } else if (this.changeStatus[0] === '1') {
        return '/personal/edit/contacts/change'
      } else {
        console.log('changeStatus === \'22\'')
      }
    }
  },
  methods: {
    // 提交验证
    onSubmit (href) {
      // 阻止重复提交
      if (!this.allowSubmit) {
        return
      }
      this.allowSubmit = false
      // 新提交身份认证set
      if (this.type === 'set') {
        // 获取需要上传的数据
        let idenData = {
          id_card_front_side_url: this.identityInfo.identifyCard,
          student_front_side_url: this.identityInfo.studentCard,
          holding_id_card_url: this.identityInfo.selfHoldCard,
          now_address: this.identityInfo.currentAddress,
          grade: this.identityInfo.grade,
          school_name: this.identityInfo.school,
          school_district: this.identityInfo.schoolDistrict
        }
        this.setIden(idenData, href)
      // 更新身份认证信息update || 更换身份认证信息change
      } else {
        let identityData = {
          id_card_front_side_url: this.identityInfo.identifyCard,
          student_front_side_url: this.identityInfo.studentCard,
          holding_id_card_url: this.identityInfo.selfHoldCard,
          now_address: this.identityInfo.currentAddress,
          grade: this.identityInfo.grade,
          school_name: this.identityInfo.school,
          school_district: this.identityInfo.schoolDistrict
        }
        // 提交更新
        this.updateIdenConfirm(identityData, href)
      }
    },
    // 获取更新认证信息update
    getUpdateData () {
      let t = window.localStorage.getItem('thaiPayMallLanguage')
      var languageType
      if (t) {
        if (t === 'cn') {
          languageType = 1
        } else if (t === 'en') {
          languageType = 3
        } else {
          languageType = 2
        }
      } else {
        languageType = 2
      }
      this.$ajax.getDatedIndentity(languageType).then(res => {
        if (res.code === 200) {
          res.data.forEach((item, index) => {
            if (item.type === 'credit_identificationscanning') {
              this.identityInfo = {
                firstName: item.surname,
                lastName: item.name,
                sex: item.sex === 0 ? 'female'
                   : item.sex === 1 ? 'male'
                   : '',
                identityNumber: item.id_num,
                birthDate: item.birth_date,
                identityAddress: item.now_address,
                // startDate: item.id_start,
                endDate: item.id_end,
                currentAddress: item.now_address,
                frontOfIDCard: ''
              }
              this.identityInfoID = item.id
              this.warningIden = this.$t('message.theReason') + this.$t('message.personalIdentitySchoolInfoTip')
            } else {
              console.log('getDatedIndentity()数据错误')
            }
          })
          console.log('Get update data')
        } else {
          console.log(res.code, res.msg)
        }
      }).catch(err => {
        this.$store.commit('alert', {
          switch: true,
          alertText: this.$t('message.errorMessage')
        })
      })
    },
    // 获取修改认证信息change
    getChangeData () {
      let t = window.localStorage.getItem('thaiPayMallLanguage')
      var languageType
      if (t) {
        if (t === 'cn') {
          languageType = 1
        } else if (t === 'en') {
          languageType = 3
        } else {
          languageType = 2
        }
      } else {
        languageType = 2
      }
      this.$ajax.getFailIndentity(languageType).then(res => {
        if (res.code === 200) {
          let warningTxt = []
          res.data.forEach((item, index) => {
            if (item.type === 'credit_identificationscanning') {
              this.identityInfo.currentAddress = item.now_address
              // this.identityInfo.frontOfIDCard = item.file
              this.identityInfo.identifyCard = ''
              this.identityInfoID = item.id
              // 警告信息
              warningTxt.push(!item.msg || item.msg === '' ? this.$t('message.none') : item.msg)
            } else if (item.type === 'credit_graducation') {
              this.identityInfo.school = item.school_name
              this.identityInfo.schoolDistrict = item.school_district
              this.identityInfo.grade = item.grade
              // this.identityInfo.photo = item.file
              this.identityInfo.studentCard = ''
              this.studentInfoID = item.id
              // 警告信息
              warningTxt.push(!item.msg || item.msg === '' ? this.$t('message.none') : item.msg)
            } else {
              console.log('响应数据的type字段读取失败')
            }
          })
          this.warningIden = this.$t('message.theReason') + ' ' + warningTxt.join(' / ')
          console.log('Get change data')
        } else if (res.code === 301) {
          console.log('Sign in again')
        } else {
          console.log('Something is wrong')
        }
      })/* .catch(err => {
        this.$store.commit('alert', {
          switch: true,
          alertText: this.$t('message.errorMessage')
        })
      }) */
    },
    // 从身份验证子组件获取数据
    getIdentityInfo (data) {
      if (data === 'no') {
        this.allowSubmitIden = false
      } else {
        this.identityInfo = data
        this.allowSubmitIden = true
      }
    },
    // 请求 set 身份认证
    setIden (identityData, href) {
      this.$ajax.proveData(identityData).then(res => {
        if (res.code === 200) {
          console.log('上传成功')
          this.$router.push(href)
        } else {
          console.log('setIden is wrong')
        }
        this.allowSubmit = true
      })/* .catch(err => {
        this.$store.commit('alert', {
          switch: true,
          alertText: this.$t('message.errorMessage')
        })
      }) */
    },
    updateIdenConfirm (identityData, href) {
      this.$ajax.updateIdentity(identityData).then(res => {
        if (res.code === 200) {
          console.log('上传身份认证信息成功')
          this.$router.push(href)
        } else {
          console.log(res.code, res.msg)
        }
        this.allowSubmit = true
      }).catch(err => {
        this.$store.commit('alert', {
          switch: true,
          alertText: this.$t('message.errorMessage')
        })
      })
    },
    // 获取用户全部认证状态
    getAuthenStatus () {
      this.$ajax.getAuthenStatus().then(res => {
        if (res.code === 200) {
          this.changeStatus = `${res.data.con_status}`
        } else {
          console.log(res.code, res.msg)
        }
      })/* .catch(err => {
        this.$store.commit('alert', {
          switch: true,
          alertText: this.$t('message.errorMessage')
        })
      }) */
    }
  },
  mounted () {
    this.getAuthenStatus()
    if (this.type === 'update') {
      this.getUpdateData()
    } else if (this.type === 'change') {
      this.getChangeData()
    }
    window.scrollTo(0, 0)
  }
}
</script>

<style scoped>
.authentication-warning {
	width: 805px;
	height: 50px;
	margin: 40px auto 0;
	line-height: 50px;
	font-size: 14px;
	border: 1px solid #ff600a;
	padding-left: 55px;
	background: #fffdf4 url(../../../assets/images/icon_errorTips.png) no-repeat 23px center;
}

.authentication-content {
	margin-top: 60px;
}

.v-identity-component {
  width: 861px;
  margin: 0 auto;
}

.authentication-tip {
	height: 20px;
	line-height: 20px;
	font-size: 14px;
	color: #ff600a;
	margin-top: 12px;
	padding-left: 24px;
	background: url(../../../assets/images/icon_errorTips.png) no-repeat left center;
}

.btn-submit {
	height: 48px;
  margin-top: 80px;
	text-align: center;
}

.btn-submit > button {
	width: 376px;
	height: 48px;
	border: none;
	background-color: #ff600a;
	color: #fff;
	border-radius: 2px;
	cursor: pointer;
}

.btn-forbid-item {
  background-color: #d7d7d7 !important;
  cursor: not-allowed !important;
}
</style>
